﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TripLOAMobile
{
    public partial class TempShowDetails : Form
    {
        private bool isBookable;
        
        
        private int idTrip;

        private bool changesAboutFavorites,changesAboutBooking;
        public bool NeedToReloadTheList//da rivedere...
        {
            get
            {
                return changesAboutFavorites||changesAboutBooking;
            }
        }

        private WS_SoapTrip.TripInfo ActualTrip
        {
            set
            {
                //actualTrip = value;
                this.idTrip=value.ID;
                label8.Text = value.Departure;
                label9.Text = value.Arrival;
                label10.Text = value.DepartureTime.ToString("hh:mm");
                label15.Text = value.DepartureTime.ToString("dd/MM/yyyy");
                label11.Text = value.EstimatedTime.ToString();
                label12.Text = value.EstimatedCost.ToString();
                label13.Text = value.AcceptSmokers ? "Accepted" : "Not accepted";
                label14.Text = value.FreeSeatNumber.ToString();
                System.Diagnostics.Debug.WriteLine("id del viaggio="+this.idTrip);
            }
        }

        
        /*public TempShowDetails()
        {
            InitializeComponent();
            //visualizza "nessun viaggio selezionato"...
        }*/

        public TempShowDetails(TripLOAMobile.WS_SoapTrip.TripInfo details, /*int id da levare ID,*/ bool isBookable)
        {
            InitializeComponent();
            //details.ID = id;
            /*this.changesAboutFavorites = false;
            this.changesAboutBooking = false;
            this.isBookable = isBookable;
            if (!this.isBookable)
            {
                this.mainMenu1.MenuItems.Clear();
            }
            else if (Welcome.rrp.isBooked(details.ID))
            {
                this.setBookButtonMeaning(true);
            }
            else if (Welcome.rrp.isFavorite(details.ID))
            {
                this.setFavoritesButtonMeaning(true);
            }
            else
            {
                this.setStandardButtonsMeaning();
            }
            ActualTrip = details;
             */
        }

        #region methods about booking

        private void bookTravel(object sender, EventArgs e)
        {
            bool bookingSucceeded=Welcome.rrp.SendBookingTravelRequest(this.idTrip);
            if (bookingSucceeded)
            {
                //prenotazione ha avuto successo...
                this.setBookButtonMeaning(true);//disattiva bottone per prenotare/trasformalo in "rimuovi prenotazione..."
                //quando si tornerà nella lista di partenza bisogna ricaricarla...
                this.changesAboutBooking = !this.changesAboutBooking;

                //MOSTRA MESSAGGIO...
                //MessageBox.Show("booking succeeded");
            }
            else
            {
                //prenotazione NON ha avuto successo...gestire...
                //MOSTRA MESSAGGIO...
                //MessageBox.Show("booking failed");
            }
        }

        private void removeBook(object sender, EventArgs e)
        {
            if (Welcome.rrp.SendCancelBookingRequest(this.idTrip))
            {
                this.setBookButtonMeaning(false);
                this.changesAboutBooking = !this.changesAboutBooking;
            }
        }

        private void setBookButtonMeaning(bool isBooked)
        {
            if (!this.isBookable)
                return;
            if (isBooked)
            {
                //metti bottone per rimuovere prenotazione
                this.booksMenuItem.Text = "Remove Book";
                this.booksMenuItem.Click -= new System.EventHandler(this.bookTravel);
                this.booksMenuItem.Click += new System.EventHandler(this.removeBook);

                //this.addFavoritesMenuItem.Text = "";
                this.addFavoritesMenuItem.Enabled = false;
            }
            else
            {
                //metti bottone per prenotare
                this.booksMenuItem.Text = "Book";
                this.booksMenuItem.Click -= new System.EventHandler(this.removeBook);
                this.booksMenuItem.Click += new System.EventHandler(this.bookTravel);

                this.addFavoritesMenuItem.Enabled = true;
            }
            Invalidate();
            Update();
        }

        #endregion

        #region methods about favorites

        private void addFavoritesMenuItem_Click(object sender, EventArgs e)
        {
            bool additionSucceeded = Welcome.rrp.SendAddToFavoritesRequest(this.idTrip);
            if (additionSucceeded)
            {
                //prenotazione ha avuto successo...
                this.setFavoritesButtonMeaning(true);
                //quando si tornerà nella lista di partenza bisogna ricaricarla...
                this.changesAboutFavorites = !this.changesAboutFavorites;

                //MOSTRA MESSAGGIO...
                //MessageBox.Show("booking succeeded");
            }
            else
            {
                //prenotazione NON ha avuto successo...gestire...
                //MOSTRA MESSAGGIO...
                //MessageBox.Show("booking failed");
            }
        }

        private void removeFromFavorites_Click(object sender, EventArgs e)
        {
            if (Welcome.rrp.SendRemoveFromFavoritesRequest(this.idTrip))
            {
                this.setFavoritesButtonMeaning(false);
                this.changesAboutFavorites = !this.changesAboutFavorites;
            }
        }

        private void setFavoritesButtonMeaning(bool isFavorite)
        {
            if (!this.isBookable)
                return;
            if (isFavorite)
            {
                this.addFavoritesMenuItem.Text = "Remove from favorites";
                this.addFavoritesMenuItem.Click -= new System.EventHandler(this.addFavoritesMenuItem_Click);
                this.addFavoritesMenuItem.Click += new System.EventHandler(this.removeFromFavorites_Click);

                this.booksMenuItem.Enabled = false;
            }
            else
            {
                this.addFavoritesMenuItem.Text = "Add to favorites";
                this.booksMenuItem.Click -= new System.EventHandler(this.removeFromFavorites_Click);
                this.addFavoritesMenuItem.Click += new System.EventHandler(this.addFavoritesMenuItem_Click);
                this.booksMenuItem.Enabled = true;
            }
            Invalidate();
            Update();
        }


        #endregion

        private void setStandardButtonsMeaning()
        {
            if (!this.isBookable)
                return;
            //metti bottone per prenotare
            this.booksMenuItem.Text = "Book";
            this.booksMenuItem.Click -= new System.EventHandler(this.removeBook);
            this.booksMenuItem.Click += new System.EventHandler(this.bookTravel);

            //metti bottone per aggiungere ai favoriti
            this.addFavoritesMenuItem.Text = "Add to facorites";
            this.booksMenuItem.Click -= new System.EventHandler(this.removeFromFavorites_Click);
            this.addFavoritesMenuItem.Click += new System.EventHandler(this.addFavoritesMenuItem_Click);
        }   
    }
}